<template>
  <div class="regist">
    <div class="user">您的推荐人</div>
    <div v-if="tuijianren" class="tuijian">
      {{ tuijianren.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') }}
    </div>
    <div class="phonebox">
      <van-field
        v-model="phone"
        class="vaninput"
        maxlength="11"
        type="digit"
        placeholder="请输入您的手机号"
      />
    </div>
    <div class="codebox">
      <van-field
        v-model="code"
        :border="false"
        class="vaninput"
        maxlength="6"
        type="digit"
        placeholder="请输入验证码"
      />
      <div v-if="!start" class="getcode clickbg" @click="getcode">
        获取验证码
      </div>
      <van-count-down
        v-if="start"
        ref="countDown"
        millisecond
        :time="1000 * 60"
        :auto-start="true"
        format="ss"
        @finish="start = false"
      >
        <template #default="timeData">
          <span style="color: #fff; font-size: 15px">
            {{ timeData.seconds }}s后再次获取
          </span>
        </template>
      </van-count-down>
    </div>
    <div class="btns flexCenter clickbg" @click="onsubmit">立即注册</div>
  </div>
</template>

<script>
  export default {
    name: 'Register',
    data() {
      return {
        phone: '',
        code: '',
        start: false,
        tuijianren: '',
      }
    },
    created() {
      this.tuijianren = this.$route.query.phone
    },
    methods: {
      onsubmit() {
        this.$api
          .register({
            fatherContactInformation: this.tuijianren,
            contactInformation: this.phone,
            code: this.code,
          })
          .then((res) => {
            this.$toast(res.msg)
            this.start = false
            this.phone = ''
            this.code = ''
            this.$router.push('/download')
          })
          .catch((err) => {
            this.$toast(err.msg)
          })
      },
      getcode() {
        if (!this.phone.length) return this.$toast.fail('手机号不能位空')
        this.$api
          .getCheckCode({
            contactInformation: this.phone,
            type: 1,
          })
          .then((res) => {
            this.start = true
            this.$toast(res.msg)
          })
          .catch((err) => {
            this.$toast(err.msg)
          })
      },
    },
  }
</script>

<style lang="less" scoped>
  .regist {
    flex: 1;
    background: url('../assets/imgs/bg.png') no-repeat;
    background-size: 100% 100%;
    text-align: center;
    padding: 41px 28px 0;
    overflow: auto;
    .user {
      font-size: 15px;
      font-weight: 500;
      color: #ffffff;
    }
    .tuijian {
      font-size: 24px;
      font-weight: bold;
      color: #ffffff;
      margin-top: 26px;
    }
    .phonebox {
      margin-top: 45px;
      border-bottom: 1px solid #dfe5eb;
      .vaninput {
        background: transparent;
        padding: 22px 0;
        text-index: 2px;
        color: #ffffff;
        font-size: 15px;
        /deep/input {
          color: #ffffff;
        }
        ::placeholder {
          color: #ffffff;
        }
      }
    }
    .codebox {
      margin-top: 18px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #dfe5eb;
      .vaninput {
        flex: 1;
        background: transparent;
        padding: 22px 0;
        text-index: 2px;
        color: #ffffff;
        font-size: 15px;
        /deep/input {
          color: #ffffff;
        }
        ::placeholder {
          color: #ffffff;
        }
      }
      .getcode {
        font-size: 15px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #ffffff;
        margin-right: 8px;
      }
    }
    .btns {
      margin: auto;
      margin-top: 58px;
      width: 190px;
      height: 44px;
      background: #ffffff;
      border-radius: 6px;
      font-weight: bold;
      color: #2c355c;
      font-size: 15px;
    }
  }
</style>
